%% @doc templ_eval のテスト
%%
-module(t_templ_eval).
-import(templ, [init/0,init/1,process/1,process/2,process/3]).

-include_lib("eunit/include/eunit.hrl").

process_if_test_() ->
	init(),

	[
	 ?_assertMatch("hogefoo", 
				   process("hoge{if true}foo{endif}")),
	 ?_assertMatch("hoge", 
				   process("hoge{if (false)}foo{end}")),
	 ?_assertMatch("hogefoo", 
				   process("hoge{if true}foo{else}bar{endif}")),
	 ?_assertMatch("hogebar", 
				   process("hoge{if false}foo{else}bar{endif}")),
	 ?_assertMatch("hogefooasdf", 
				   process("hoge{if true}foo{if true}asdf{end}{else}bar{endif}")),
	 ?_assertMatch("hoge123", 
				   process("hoge{if true}{123}{endif}")),
	 ?_assertMatch("hogezero123four", 
				   process("hoge{if true}zero{123}four{endif}", [])),

	 ?_assertMatch("foo", 
				   process("{if 1}foo{endif}{if 0}bar{endif}")),
	 ?_assertMatch("", 
				   process("{if foo}hoge{endif}")),
	 ?_assertMatch("hoge", 
				   process("{if foo}hoge{endif}", [{"foo", true}])),
	 ?_assertError(_, 
				   process("{if foo}hoge{endif}", [{"foo", asdf}]))
	].

process_let_test_() ->
	[
	 ?_assertMatch("1", 
				   process("{let a is 1}{a}{end}")),
	 ?_assertMatch("foo1", 
				   process("{let hoge is 1}foo{hoge}{endlet}")),
	 ?_assertMatch("foo1bar", 
				   process("{let a is 1}foo{a}bar{endlet}")),
	 ?_assertMatch("foobar", 
				   process("{let a is \"bar\"}foo{a}{endlet}")),

	 ?_assertError({novalue,"a"}, process("{let a is \"bar\"}foo{end}{a}")),
	 ?_assertError(_, process("{let a is \"bar\"}foo{a}{end}", [{"a", 123}])),
	 ?_assertMatch("", process("{let a is bar}foo{a}{end}"))
	].

process_foreach_test_() ->
	[
	 ?_assertMatch("12345", 
				   process("{foreach a in list}{a}{end}",
						   [{"list", [1, 2, 3, 4, 5]}])),
	 ?_assertMatch("a:123, b:123, c:123", 
				   process("{foreach v1 in list1}{v1}:{foreach v2 in list2}{v2}{end}{if v1 != \"c\"}, {end}{end}",
						   [{"list1", ["a", "b", "c"]}, {"list2", [1, 2, 3]}])),

	 ?_assertError(_, 
				   process("{foreach a in list}{a}{end}",
						   [{"a", asdf}, {"list", [1, 2, 3, 4, 5]}])),
	 ?_assertMatch("", 
				   process("{foreach a in hoge}{a}{end}"))
	].
